package com.zhuss.product.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhuss.bean.vo.product.hotLine.ListHotLineVoEx;
import com.zhuss.bean.vo.product.hotLineSite.ListHotLineSiteVo;
import com.zhuss.product.db.dao.ext.HotLineSiteDao;
import com.zhuss.product.db.dao.ext.SiteDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.hotLine.SearchHotLineBo;
import com.zhuss.bean.vo.product.hotLine.ListHotLineVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.product.db.dao.ext.HotLineDao;
import com.zhuss.product.db.entity.HotLine;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;


/**
 * @description: 热门线路读
 * @author: zhushanshuo
 * @create: Mon Jan 11 09:20:35 CST 2021
 **/
@Component("hotLineRead")
public class HotLineRead {
	
	@Autowired
	private HotLineDao hotLineDao;
	@Autowired
	private HotLineSiteDao hotLineSiteDao;
	@Autowired
	private SiteDao siteDao;

	public Map<String, HotLine> listMap() {
		List<HotLine> list = hotLineDao.listAll();
		Map<String, HotLine> map = new HashMap<>();
		for (HotLine c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}
	
	/**
	 * 热门线路
	 * @return
	 */
	public Object listHotLine() {
		List<HotLine> hotLineList = hotLineDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(hotLineList, result);
		return result;
	}
	
	
	/**
	 * 热门线路
	 * @return
	 */
	public Object listHotLine(List<Long> hotLineIdList) {
		List<HotLine> result = hotLineDao.listById(hotLineIdList);
		return result;
	}
	
	
	/**
	 * 根据名字查询热门线路
	 * @param 
	 * @return
	 */
	public List<HotLine> list(String name) {
		List<HotLine> result = hotLineDao.listByName(name);
		return result;
	}
	
	public List<HotLine> listNotId(String name, Long hotLineId) {
		List<HotLine> result = new ArrayList<HotLine>();
		List<HotLine> list = list(name);
		for (HotLine c : list) {
			if (!c.getHotLineId().equals(hotLineId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 热门线路 
	 * @return
	 */
	public Object listHotLineId() {
		List<HotLine> hotLineList = hotLineDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(hotLineList, result);
		return result;
	}
	
	private void transferListId(List<HotLine> hotLineList, List<Long> result) {
		for (HotLine item : hotLineList) {
			result.add(item.getHotLineId());
		}
	}

	private void transferList(List<HotLine> hotLineList, List<ListVo> result) {
		for (HotLine item : hotLineList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getHotLineId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 热门线路列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchHotLineBo bo) {
		PageVo<ListHotLineVoEx> result = new PageVo<ListHotLineVoEx>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
		List<ListHotLineVoEx> list = hotLineDao.hotLinelistex(bo);
		if(list!=null) {
			for (ListHotLineVoEx hotline : list) {
				List<ListHotLineSiteVo> sitelist=hotLineSiteDao.listbyHotLineId(hotline.getHotLineId());
				for (ListHotLineSiteVo site: sitelist) {
					site.setName(siteDao.selectByPrimaryKey(site.getSiteId()).getName());
				}
				hotline.setSiteList(sitelist);
			}
		}
        result.setItems(list);
        result.setTotal(hotLineDao.countList(bo));
		return result;
	}
	
	/**
	 * 热门线路详情
	 * @param hotLineId
	 * @return
	 * @throws DzcxException
	 */
	public Object detail(Long hotLineId) throws DzcxException {
		Object result = hotLineDao.detail(hotLineId);
		if (result == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		}
		return result;
	}

	/**
	 * 根据线路查询绑定的站点
	 * @param hotLineId
	 * @return
	 * @throws DzcxException
	 */
    public Object bindSiteList(Long hotLineId) throws DzcxException {
    	List<ListHotLineSiteVo> res  = hotLineSiteDao.listbyHotLineId(hotLineId);
		if (res == null) {
			throw new DzcxException(ExceptionCode.数据不存在);
		} else {
			res.forEach(siteVo -> {
				siteVo.setName(siteDao.selectByPrimaryKey(siteVo.getSiteId()).getName());
			});
		}
		return res;
    }
}
